﻿package org.xhs.ui.controls
{
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class Button extends BUI
    {
        protected var background:DisplayObject;
        protected var _mouseState:String;
        protected var _selected:Boolean;
        private var _mouseStateLocked:Boolean;
        private var unlockedMouseState:String;
        protected var _autoRepeat:Boolean;
        protected var pressTimer:Timer;
        public static const frameDict:Object = {up:1, over:2, down:3, disabled:4, selectedUp:5, selectedOver:6, selectedDown:7, selectedDisabled:8};
        public static var defaultStyles:Object = {skin:"Button_Skin", repeatDelay:500, repeatInterval:50};

        public function Button()
        {
            buttonMode = true;
            mouseChildren = false;
            useHandCursor = false;
            this._mouseState = "up";
            _enabled = true;
            this._selected = false;
            this._mouseStateLocked = false;
            this._autoRepeat = false;
            this.setupMouseEvents();
            this.pressTimer = new Timer(1, 0);
            this.pressTimer.addEventListener(TimerEvent.TIMER, this.buttonDown, false, 0, true);
            return;
        }// end function

        override public function get classStyles() : Object
        {
            return BUI.mergeStyles(super.classStyles, defaultStyles);
        }// end function

        public function get mouseState() : String
        {
            return this._mouseState;
        }// end function

        public function set mouseState(param1:String) : void
        {
            if (this._mouseStateLocked)
            {
                this.unlockedMouseState = param1;
                return;
            }
            if (param1 == this._mouseState)
            {
                return;
            }
            this._mouseState = param1;
            invalidate(Invalidation.STATE);
            return;
        }// end function

        public function set mouseStateLocked(param1:Boolean) : void
        {
            this._mouseStateLocked = param1;
            if (param1 == false)
            {
                this.mouseState = this.unlockedMouseState;
            }
            else
            {
                this.unlockedMouseState = this.mouseState;
            }
            return;
        }// end function

        override public function get enabled() : Boolean
        {
            return super.enabled;
        }// end function

        override public function set enabled(param1:Boolean) : void
        {
            super.enabled = param1;
            mouseEnabled = param1;
            return;
        }// end function

        public function get selected() : Boolean
        {
            return this._selected;
        }// end function

        public function set selected(param1:Boolean) : void
        {
            if (param1 == this._selected)
            {
                return;
            }
            this._selected = param1;
            invalidate(Invalidation.STATE);
            return;
        }// end function

        public function get autoRepeat() : Boolean
        {
            return this._autoRepeat;
        }// end function

        public function set autoRepeat(param1:Boolean) : void
        {
            this._autoRepeat = param1;
            return;
        }// end function

        override protected function draw() : void
        {
            if (isInvalid(Invalidation.STYLES, Invalidation.STATE))
            {
                this.drawBackground();
                invalidate(Invalidation.SIZE, false);
            }
            if (isInvalid(Invalidation.SIZE))
            {
                this.drawLayout();
            }
            super.draw();
            return;
        }// end function

        protected function drawLayout() : void
        {
            this.background.width = _width;
            this.background.height = _height;
            return;
        }// end function

        protected function drawBackground() : void
        {
            var _loc_1:* = this.background as MovieClip;
            this.background = getSkinInstance(getStyleValue("skin")) as MovieClip;
            addChildAt(this.background, 0);
            if (_loc_1 != null && _loc_1 != this.background && contains(_loc_1))
            {
                removeChild(_loc_1);
            }
            if (isNaN(_width))
            {
                _width = this.background.width;
            }
            if (isNaN(_height))
            {
                _height = this.background.height;
            }
            var _loc_2:* = this.enabled ? (this.mouseState) : ("disabled");
            if (this.selected)
            {
                _loc_2 = "selected" + _loc_2.substr(0, 1).toUpperCase() + _loc_2.substr(1);
            }
            (this.background as MovieClip).gotoAndStop(frameDict[_loc_2]);
            return;
        }// end function

        protected function setupMouseEvents() : void
        {
            addEventListener(MouseEvent.ROLL_OVER, this.mouseEventHandler, false, 0, true);
            addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler, false, 0, true);
            addEventListener(MouseEvent.MOUSE_UP, this.mouseEventHandler, false, 0, true);
            addEventListener(MouseEvent.ROLL_OUT, this.mouseEventHandler, false, 0, true);
            return;
        }// end function

        protected function mouseEventHandler(event:MouseEvent) : void
        {
            if (event.type == MouseEvent.MOUSE_DOWN)
            {
                this.mouseState = "down";
                this.startPress();
            }
            else if (event.type == MouseEvent.ROLL_OVER || event.type == MouseEvent.MOUSE_UP)
            {
                this.mouseState = "over";
                this.endPress();
            }
            else if (event.type == MouseEvent.ROLL_OUT)
            {
                this.mouseState = "up";
                this.endPress();
            }
            return;
        }// end function

        protected function startPress() : void
        {
            if (this._autoRepeat)
            {
                this.pressTimer.delay = Number(getStyleValue("repeatDelay"));
                this.pressTimer.start();
            }
            dispatchEvent(new BUIEvent(BUIEvent.BUTTON_REPEAT, true));
            return;
        }// end function

        protected function endPress() : void
        {
            this.pressTimer.reset();
            return;
        }// end function

        protected function buttonDown(event:TimerEvent) : void
        {
            if (!this._autoRepeat)
            {
                this.endPress();
                return;
            }
            if (this.pressTimer.currentCount == 1)
            {
                this.pressTimer.delay = Number(getStyleValue("repeatInterval"));
            }
            dispatchEvent(new BUIEvent(BUIEvent.BUTTON_REPEAT, true));
            return;
        }// end function

    }
}
